package com.lyc.greendao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.lyc.callerinfo.model.db.HyBean;

import com.lyc.callerinfo.model.db.InCallBean;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "IN_CALL_BEAN".
*/
public class InCallBeanDao extends AbstractDao<InCallBean, Long> {

    public static final String TABLENAME = "IN_CALL_BEAN";

    /**
     * Properties of entity InCallBean.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property Iszhapian = new Property(1, String.class, "iszhapian", false, "ISZHAPIAN");
        public final static Property Province = new Property(2, String.class, "province", false, "PROVINCE");
        public final static Property City = new Property(3, String.class, "city", false, "CITY");
        public final static Property Sp = new Property(4, String.class, "sp", false, "SP");
        public final static Property Phone = new Property(5, String.class, "phone", false, "PHONE");
        public final static Property Rpt_type = new Property(6, String.class, "rpt_type", false, "RPT_TYPE");
        public final static Property Rpt_comment = new Property(7, String.class, "rpt_comment", false, "RPT_COMMENT");
        public final static Property Rpt_cnt = new Property(8, String.class, "rpt_cnt", false, "RPT_CNT");
        public final static Property Hyname = new Property(9, String.class, "hyname", false, "HYNAME");
        public final static Property CountDesc = new Property(10, String.class, "countDesc", false, "COUNT_DESC");
        public final static Property HyId = new Property(11, Long.class, "hyId", false, "HY_ID");
    }

    private DaoSession daoSession;


    public InCallBeanDao(DaoConfig config) {
        super(config);
    }
    
    public InCallBeanDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"IN_CALL_BEAN\" (" + //
                "\"_id\" INTEGER PRIMARY KEY ," + // 0: id
                "\"ISZHAPIAN\" TEXT," + // 1: iszhapian
                "\"PROVINCE\" TEXT," + // 2: province
                "\"CITY\" TEXT," + // 3: city
                "\"SP\" TEXT," + // 4: sp
                "\"PHONE\" TEXT NOT NULL ," + // 5: phone
                "\"RPT_TYPE\" TEXT," + // 6: rpt_type
                "\"RPT_COMMENT\" TEXT," + // 7: rpt_comment
                "\"RPT_CNT\" TEXT," + // 8: rpt_cnt
                "\"HYNAME\" TEXT," + // 9: hyname
                "\"COUNT_DESC\" TEXT," + // 10: countDesc
                "\"HY_ID\" INTEGER);"); // 11: hyId
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"IN_CALL_BEAN\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, InCallBean entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String iszhapian = entity.getIszhapian();
        if (iszhapian != null) {
            stmt.bindString(2, iszhapian);
        }
 
        String province = entity.getProvince();
        if (province != null) {
            stmt.bindString(3, province);
        }
 
        String city = entity.getCity();
        if (city != null) {
            stmt.bindString(4, city);
        }
 
        String sp = entity.getSp();
        if (sp != null) {
            stmt.bindString(5, sp);
        }
        stmt.bindString(6, entity.getPhone());
 
        String rpt_type = entity.getRpt_type();
        if (rpt_type != null) {
            stmt.bindString(7, rpt_type);
        }
 
        String rpt_comment = entity.getRpt_comment();
        if (rpt_comment != null) {
            stmt.bindString(8, rpt_comment);
        }
 
        String rpt_cnt = entity.getRpt_cnt();
        if (rpt_cnt != null) {
            stmt.bindString(9, rpt_cnt);
        }
 
        String hyname = entity.getHyname();
        if (hyname != null) {
            stmt.bindString(10, hyname);
        }
 
        String countDesc = entity.getCountDesc();
        if (countDesc != null) {
            stmt.bindString(11, countDesc);
        }
 
        Long hyId = entity.getHyId();
        if (hyId != null) {
            stmt.bindLong(12, hyId);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, InCallBean entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String iszhapian = entity.getIszhapian();
        if (iszhapian != null) {
            stmt.bindString(2, iszhapian);
        }
 
        String province = entity.getProvince();
        if (province != null) {
            stmt.bindString(3, province);
        }
 
        String city = entity.getCity();
        if (city != null) {
            stmt.bindString(4, city);
        }
 
        String sp = entity.getSp();
        if (sp != null) {
            stmt.bindString(5, sp);
        }
        stmt.bindString(6, entity.getPhone());
 
        String rpt_type = entity.getRpt_type();
        if (rpt_type != null) {
            stmt.bindString(7, rpt_type);
        }
 
        String rpt_comment = entity.getRpt_comment();
        if (rpt_comment != null) {
            stmt.bindString(8, rpt_comment);
        }
 
        String rpt_cnt = entity.getRpt_cnt();
        if (rpt_cnt != null) {
            stmt.bindString(9, rpt_cnt);
        }
 
        String hyname = entity.getHyname();
        if (hyname != null) {
            stmt.bindString(10, hyname);
        }
 
        String countDesc = entity.getCountDesc();
        if (countDesc != null) {
            stmt.bindString(11, countDesc);
        }
 
        Long hyId = entity.getHyId();
        if (hyId != null) {
            stmt.bindLong(12, hyId);
        }
    }

    @Override
    protected final void attachEntity(InCallBean entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public InCallBean readEntity(Cursor cursor, int offset) {
        InCallBean entity = new InCallBean( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // iszhapian
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // province
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // city
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // sp
            cursor.getString(offset + 5), // phone
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // rpt_type
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // rpt_comment
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // rpt_cnt
            cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9), // hyname
            cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10), // countDesc
            cursor.isNull(offset + 11) ? null : cursor.getLong(offset + 11) // hyId
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, InCallBean entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setIszhapian(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setProvince(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setCity(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setSp(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setPhone(cursor.getString(offset + 5));
        entity.setRpt_type(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setRpt_comment(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setRpt_cnt(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setHyname(cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9));
        entity.setCountDesc(cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10));
        entity.setHyId(cursor.isNull(offset + 11) ? null : cursor.getLong(offset + 11));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(InCallBean entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(InCallBean entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(InCallBean entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getHyBeanDao().getAllColumns());
            builder.append(" FROM IN_CALL_BEAN T");
            builder.append(" LEFT JOIN HY_BEAN T0 ON T.\"HY_ID\"=T0.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected InCallBean loadCurrentDeep(Cursor cursor, boolean lock) {
        InCallBean entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        HyBean bean = loadCurrentOther(daoSession.getHyBeanDao(), cursor, offset);
        entity.setBean(bean);

        return entity;    
    }

    public InCallBean loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<InCallBean> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<InCallBean> list = new ArrayList<InCallBean>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<InCallBean> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<InCallBean> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
